<?php
/**
 * Created by PhpStorm.
 * User: CCA
 * Date: 2019/10/17
 * Time: 14:49
 */

namespace app\api\controller;


use app\api\BaseController;
use app\common\logic\Memberevaluate;
use app\common\model\Evaluategoods;
use think\Lang;

/**
 * @title 评价
 */
class Comment extends BaseController
{

    public function _initialize()
    {
        parent::_initialize();
        Lang::load(APP_PATH . 'home/lang/' . config('default_lang') . '/memberevaluate.lang.php');
    }

    /**
     * @title 添加评价
     * @description
     * @param string anony 匿名off为关闭on为开 ''
     * @param array goods 商品列表数组格式goods[goodsid][score]/goods[goodsid][comment] ''
     * @param string score 平均分(5分制) ''
     * @return {"code":200,"msg":"succeess"}
     * @author fuzz
     * @url /Comment/add/order_id/{value}
     * @method POST
     *
     * @code 200 成功
     * @code 201 失败
     *
     */
    public function add()
    {
        //晒单图片 没有入库  看前台传的值再入库
        $order_id = intval(input('order_id'));
        $goods_id = intval(input('goods_id'));
        $rec_id = intval(input('rec_id'));
        $evaluation_type = intval(input('evaluation_type'));
        if (!$order_id || empty($evaluation_type)) {
            return json_fail_return('参数错误');
        }

        $memberValudate = new Memberevaluate();
        //判断类型  因为商城需要的参数多
        if ($evaluation_type == 1) {
            $return = $memberValudate->validation($order_id, $this->user_id);

            extract($return['data']);
        } elseif ($evaluation_type == 2) {
            $order_info = [];
            $store_info = [];
            $order_goods = [];
        } else {
            $order_info = db('orderproject')->where(['order_id' => $order_id])->find();
            $store_info = [];
            $order_goods = [];
        }

        $return = $memberValudate->saveorderevaluate($order_info, $store_info, $order_goods, $this->user_id, $this->user_info['member_name'], $evaluation_type, $goods_id, $rec_id, $order_id, $this->user_info);

        if ($return == true) {
            json_success_return(lang('member_evaluation_evaluat_success'));
        } else {
            json_fail_return(lang('ds_common_save_fail'));
        }
    }

    /**
     * @title 评价列表
     * @description
     * @param string type 不传查全部1好评2中评3差评
     * @param array goods 商品列表数组格式goods[goodsid][score]/goods[goodsid][comment] ''
     * @param string score 平均分 ''
     * @return {"code":200,"msg":"succeess"}
     * @author fuzz
     * @url /Comment/select/goods_id/{value}
     * @method POST
     *
     * @code 200 成功
     * @code 201 失败
     *
     */
    public function select()
    {
        $goods_id = intval(input('param.goods_id'));

        // 商品详细信息
        $goods_model = new \app\common\model\Goods();
        $goods_info = $goods_model->getGoodsInfoByID($goods_id);
        // 验证商品是否存在
        if (empty($goods_info)) {
            return json_fail_return('商品不存在');
        }

        //评价信息
        $goods_evaluate_info = (new Evaluategoods())->getEvaluategoodsInfoByGoodsID($goods_id);

        //页数
        $page = empty(intval(input('param.page'))) ? 20 : intval(input('param.page'));

        //限制20条
        $comment_list = $this->_get_comments($goods_id, input('param.type'), 20);
        foreach ($comment_list as $key => $value) {
            if ($value['anony'] == 1) {
                $comment_list[$key]['geval_frommembername'] = '匿名用户';
                $comment_list[$key]['member_avatar'] = 'http://33wang.douyinhot.com/fd4f820200425173148222.png';
            } else {
                $comment_list[$key]['member_avatar'] = 'http://33wang.douyinhot.com/' . $value['member_avatar'];
            }
        }

        $result['goods_evaluate_info'] = $goods_evaluate_info;
        $result['comment_list'] = $comment_list;
        json_success_return($result);

    }

    //筛选
    private function _get_comments($goods_id, $type, $page)
    {
        $condition = array();
        //商品id筛选
        $condition['geval_goodsid'] = $goods_id;
        switch ($type) {
            case '1':
                $condition['geval_scores'] = array('in', '5,4');
                $this->assign('type', '1');
                break;
            case '2':
                $condition['geval_scores'] = array('in', '3,2');
                $this->assign('type', '2');
                break;
            case '3':
                $condition['geval_scores'] = array('in', '1');
                $this->assign('type', '3');
                break;
            default:
                $this->assign('type', '');
                break;
        }

        //查询商品评分信息
        $evaluategoods_model = new Evaluategoods();
        $goodsevallist = $evaluategoods_model->getEvaluategoodsList($condition, $page);
        //转换图片

        foreach ($goodsevallist as $key => $value) {
            $goodsevallist[$key]['geval_goodsimage'] = empty($value['geval_goodsimage']) ?: config('qiniu_http') . '/' . $value['geval_goodsimage'];
            $goodsevallist[$key]['geval_image'] = [];
            if (!empty($value['geval_image'])) {
                $images = json_decode($value['geval_image'], true);
                foreach ($images as $k => $v) {
                    $goodsevallist[$key]['geval_image'][] = $v;
                }
            }
            $goodsevallist[$key]['geval_addtime'] = date('Y-m-d H:i:s', $value['geval_addtime']);
            $goodsevallist[$key]['member_truename'] = empty($value['member_truename']) ? '未设置' : $value['member_truename'];
        }
        return $goodsevallist;
    }

    /**
     * @title 项目定制评价列表
     * @description
     * @param string type 不传查全部1好评2中评3差评
     * @param array goods 商品列表数组格式goods[goodsid][score]/goods[goodsid][comment] ''
     * @param string score 平均分 ''
     * @return {"code":200,"msg":"succeess"}
     * @author fuzz
     * @url /Comment/select/goods_id/{value}
     * @method POST
     *
     * @code 200 成功
     * @code 201 失败
     *
     */
    public function project_select()
    {
        $project_id = intval(input('param.project_id')); #当前项目定制的id

        $page = intval(input('page')) ? intval(input('page')) : 1;
        $pagesize = intval(input('pagesize')) ? intval(input('pagesize')) : 15;
        $offset = ($page - 1) * $pagesize;

        $where['geval_projectid'] = $project_id;
        $result = db('evaluategoods')
            ->where($where)
            ->order('geval_addtime desc')
            ->limit($offset, $pagesize)
            ->select();

        $config = db('config')->where('code', 'qiniu_http')->find();
        foreach ($result as $key => $value) {
            $result[$key]['geval_image'] = [];
            if (!empty($value['geval_image'])) {
                $images = json_decode($value['geval_image'], true);
                foreach ($images as $k => $v) {
                    $result[$key]['geval_image'][] = $v;
                }
            }
            $result[$key]['geval_addtime'] = date('Y-m-d H:i:s', $value['geval_addtime']);
            if ($value['anony'] == 1) {
                $result[$key]['geval_frommembername'] = '匿名用户';
                $result[$key]['member_avatar'] = $config['value'] . '/fd4f820200425173148222.png';
            }else{
                $member_info = db('member')->where(['member_id' => $value['geval_frommemberid']])->find();
                $result[$key]['member_avatar'] = $config['value'] . $member_info['member_avatar'];
            }

        }

        json_success_return($result);

    }


}